package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.OrderDetailMapper;
import csu.web.mypetstore.mapper.OrderstatusMapper;
import csu.web.mypetstore.pojo.OrderDetail;
import csu.web.mypetstore.pojo.Orders;
import csu.web.mypetstore.pojo.Orderstatus;
import csu.web.mypetstore.service.OrderDetailService;
import csu.web.mypetstore.service.OrdersService;
import csu.web.mypetstore.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 18632
 * @description 针对表【orders】的数据库操作Service实现
 * @createDate 2025-03-10 17:03:47
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService{
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderstatusMapper orderstatusMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public List<Orders> queryAllOrders() {
        return ordersMapper.selectList(null);
    }

    @Override
    public int insertOrder(Orders orders) {
       int orderId=ordersMapper.insert(orders);
       return orderId;
    }

    @Override
    public void insertOrderStatus(Orders order) {
        Orderstatus orderstatus = new Orderstatus();
        orderstatus.setOrderid(order.getOrderid());  // 修正 orderid 获取方式
        orderstatus.setLinenum(order.getLineItems().size());
        orderstatus.setTimestamp(null);
        orderstatusMapper.insert(orderstatus); // 插入 Orderstatus 记录
    }

    @Override
    public void updateOrder(Orders order) {
        ordersMapper.updateById(order);
        orderstatusMapper.delete(new LambdaQueryWrapper<Orderstatus>().eq(Orderstatus::getOrderid, order.getOrderid()));
    }

    @Override
    public void deleteOrder(int orderId) {
        ordersMapper.deleteById(orderId);
        //orderDetailMapper.delete(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderid, orderId));
        orderstatusMapper.delete(new LambdaQueryWrapper<Orderstatus>().eq(Orderstatus::getOrderid, orderId));

    }


    /**
     * 根据用户名查询订单列表
     */
    @Override
    public List<Orders> getOrdersByUsername(String username) {
        return ordersMapper.selectList(
                new LambdaQueryWrapper<Orders>()
                        .eq(Orders::getUserid, username) // 查询条件：username
        );
    }

    /**
     * 根据订单 ID 获取订单详情
     */
    @Override
    public Orders getOrder(int orderId) {

        Wrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>().eq(Orders::getOrderid, orderId);

        return ordersMapper.selectOne(wrapper);
    }

    @Override
    public void deleteOrderDetail(int orderId) {
        orderDetailMapper.delete(new LambdaQueryWrapper<OrderDetail>().eq(OrderDetail::getOrderid, orderId));
    }
}




